package org.sam.swing;

import java.awt.Component;
import java.awt.Container;
import java.awt.Window;
import java.lang.reflect.AnnotatedType;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JViewport;

import org.sam.swing.annotation.Id;

/**
 * 对象查找接口
 * 
 * @author sam
 *
 */
public interface ComponentSearch {

	public Component[] getAWTComponents();

	/**
	 * 根据id获取控件元素
	 * 
	 * @param id
	 * @return
	 */
	public default JComponent findViewById(String id) {
		Component[] components = this.getAWTComponents();
		return findViewById(components, id);
	}

	/**
	 * 查询全部的视图上控件
	 * 
	 * @return
	 */
	public default List<JComponent> findViewCompents() {

		List<JComponent> compents = new LinkedList<>();
		Component[] components = this.getAWTComponents();
		for (Component c : components) {
			AnnotatedType[] annotations = c.getClass().getAnnotatedInterfaces();
			if (annotations != null && annotations.length > 0) {
				for (AnnotatedType anno : annotations) {
					if (anno instanceof Id) {
						compents.add((JComponent) c);
					}
				}
			}
		}
		return compents;
	}

	/**
	 * 递归调用函数
	 * 
	 * @param components
	 * @param id
	 * @return
	 */
	public default JComponent findViewById(Component[] components, String id) {

		for (Component c : components) {

			if (id.equals(c.getName())) {
				return (JComponent) c;
			} else if (c instanceof JRootPane) {

				Component[] cs = ((JRootPane) c).getComponents();
				if (cs == null || cs.length <= 0)
					continue;

				JComponent find = findViewById(cs, id);
				if (find == null)
					continue;
				else
					return find;

			} else if (c instanceof JTabbedPane) {
				Component[] cs = ((JTabbedPane) c).getComponents();
				if (cs == null || cs.length <= 0)
					continue;

				JComponent find = findViewById(cs, id);
				if (find == null)
					continue;
				else
					return find;
			} else if (c instanceof JPanel) {
				Component[] cs = ((JPanel) c).getComponents();
				if (cs == null || cs.length <= 0)
					continue;

				JComponent find = findViewById(cs, id);
				if (find == null)
					continue;
				else
					return find;
			} else if (c instanceof JLayeredPane) {
				Component[] cs = ((JLayeredPane) c).getComponents();
				if (cs == null || cs.length <= 0)
					continue;

				JComponent find = findViewById(cs, id);
				if (find == null)
					continue;
				else
					return find;
			} else if (c instanceof JScrollPane) {
				Component[] cs = ((JScrollPane) c).getComponents();
				if (cs == null || cs.length <= 0)
					continue;

				JComponent find = findViewById(cs, id);
				if (find == null)
					continue;
				else
					return find;
			} else if (c instanceof JViewport) {
				Component[] cs = ((JViewport) c).getComponents();
				if (cs == null || cs.length <= 0)
					continue;

				JComponent find = findViewById(cs, id);
				if (find == null)
					continue;
				else
					return find;
			}
		}
		return null;
	}

	/**
	 * 获取父亲窗口容器
	 * @param compont
	 * @return
	 */
	public default Window findParentWindow(Component compont) {
		if (compont == null)
			return null;
		
		Container parent = compont.getParent();
		while(parent != null) {
			if (parent instanceof Window) {
				return (Window)parent;
			}
			parent = parent.getParent();
		}
		return null;
	}
	
	/**
	 * 查找父亲窗口容器的操作
	 * @return
	 */
	public default Window findParentWindow() {
		if (this instanceof Component) {
			return (this.findParentWindow((Component)this));
		}
		
		return null;
	}
}
